﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using System.Windows.Interactivity;
using Eaglight.Behaviors;
using ESRI.ArcGIS.Client.Actions;
using ESRI.ArcGIS.Client.Symbols;
using LightControls;

namespace Eaglight
{
    public class MeasureToolbar : Toolbar
    {
        private LcImagePushRadioButton btnPolygon;
        private LcImagePushRadioButton btnPolyline;
        private MeasureAction polygonMeasureAction = new MeasureAction();
        private MeasureAction polylineMeasureAction = new MeasureAction();
        private System.Windows.Interactivity.EventTrigger polygonMeasureTrigger = new System.Windows.Interactivity.EventTrigger("Click");
        private System.Windows.Interactivity.EventTrigger polylineMeasureTrigger = new System.Windows.Interactivity.EventTrigger("Click");
       
        public MeasureToolbar()
        {
            DefaultStyleKey = typeof(MeasureToolbar);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            btnPolygon = (LcImagePushRadioButton)base.GetTemplateChild("btnPolygon");
            btnPolyline = (LcImagePushRadioButton)base.GetTemplateChild("btnPolyline");
            
            controlsInitialized = true;

            if (ShowToolTips == true)
                enableToolTips();
            else
                disableToolTips();

            initActions();
            initEvents();
        }

        private void initEvents()
        {
            btnPolygon.Checked += new RoutedEventHandler(btnPolygon_Checked);
            btnPolygon.Unchecked += new RoutedEventHandler(btnPolygon_Unchecked);
            btnPolyline.Checked += new RoutedEventHandler(btnPolyline_Checked);
            btnPolyline.Unchecked += new RoutedEventHandler(btnPolyline_Unchecked);
        }

        
        void btnPolyline_Unchecked(object sender, RoutedEventArgs e)
        {
            polylineMeasureAction.IsEnabled = false;
        }

        void btnPolyline_Checked(object sender, RoutedEventArgs e)
        {
            polylineMeasureAction.IsEnabled = true;
        }

        void btnPolygon_Unchecked(object sender, RoutedEventArgs e)
        {
            polylineMeasureAction.IsEnabled = false;
        }

        void btnPolygon_Checked(object sender, RoutedEventArgs e)
        {
            polylineMeasureAction.IsEnabled = true;
        }

      
        private void initActions()
        {
            polygonMeasureAction.MeasureMode = MeasureAction.Mode.Polygon;
            polylineMeasureAction.MeasureMode = MeasureAction.Mode.Polyline;
            
            polylineMeasureAction.LineSymbol = (LineSymbol)((StackPanel)GetTemplateChild("LayoutRoot")).Resources["MeasureLineSymbol"];
            polylineMeasureAction.FillSymbol = (FillSymbol)((StackPanel)GetTemplateChild("LayoutRoot")).Resources["MeasureFillSymbol"];
            polygonMeasureAction.FillSymbol = (FillSymbol)((StackPanel)GetTemplateChild("LayoutRoot")).Resources["MeasureFillSymbol"];
            polygonMeasureAction.LineSymbol = (LineSymbol)((StackPanel)GetTemplateChild("LayoutRoot")).Resources["MeasureLineSymbol"];

            polygonMeasureTrigger.Actions.Add(polygonMeasureAction);
            polylineMeasureTrigger.Actions.Add(polylineMeasureAction);

            polygonMeasureTrigger.Attach(btnPolygon);
            polylineMeasureTrigger.Attach(btnPolyline);
        }

        protected override void enableToolTips()
        {
            if (controlsInitialized)
            {
                ToolTipService.SetToolTip(btnPolygon, "Measure Area");
                ToolTipService.SetToolTip(btnPolyline, "Measure Line");
            }
        }

        protected override void disableToolTips()
        {
            if (controlsInitialized)
            {
                ToolTipService.SetToolTip(btnPolygon, null);
                ToolTipService.SetToolTip(btnPolyline, null);
            }
        }


        protected static void onMapChanged(DependencyObject o, DependencyPropertyChangedEventArgs args)
        {
            MeasureToolbar tb = (MeasureToolbar)o;
            tb.polygonMeasureAction.TargetObject = args.NewValue;
            tb.polylineMeasureAction.TargetObject = args.NewValue;
        }

        public static readonly DependencyProperty MapProperty = DependencyProperty.Register("Map", typeof(Map), typeof(MeasureToolbar), new PropertyMetadata(new PropertyChangedCallback(onMapChanged)));

        public Map Map
        {
            get
            {
                return (Map)GetValue(MapProperty);
            }
            set
            {
                SetValue(MapProperty, value);
            }
        }

        
    }
}
